package com.rubylong.demo.service.impl;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.rubylong.demo.enums.ExportType;
import com.rubylong.demo.enums.GenerateStyle;
import com.rubylong.demo.enums.ImageType;
import com.rubylong.demo.service.Json;
import com.rubylong.demo.service.RubylongCore;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RubylongCoreImpl implements RubylongCore {

    private ActiveXComponent component;

    private Dispatch dispatch;

    protected RubylongCoreImpl() {
        ComThread.InitSTA();
        component = new ActiveXComponent("gregn.GridppReport.6");
        dispatch = component.getObject();
    }

    @Override
    public void setPrinterName(String printerName) {
        Dispatch dispatch = Dispatch.get(this.dispatch, "Printer").getDispatch();
        Dispatch.put(dispatch, "PrinterName", printerName);
    }

    @Override
    public String getPrinterName() {
        Dispatch dispatch = Dispatch.get(this.dispatch, "Printer").getDispatch();
        return Dispatch.get(dispatch, "PrinterName").getString();
    }

    @Override
    public void release() {
        ComThread.Release();
    }

    @Override
    public void fnPrintPreview(boolean showModal) {
        Dispatch.call(dispatch, "PrintPreview", new Variant(showModal));
    }

    @Override
    public void fnPrintEx(GenerateStyle generateStyle, boolean showPrintDialog) {
        Dispatch.call(dispatch, "PrintEx", new Variant(generateStyle.getValue()), new Variant(showPrintDialog));
    }

    @Override
    public void fnPrint(boolean showPrintDialog) {
        Dispatch.call(dispatch, "Print", new Variant(showPrintDialog));
    }


    @Override
    public boolean fnLoadDataFromURL(String url) {
        Variant variant = Dispatch.call(dispatch, "LoadDataFromURL", new Variant(url));
        return variant.getBoolean();
    }

    @Override
    public boolean fnLoadDataFromXML(String data) {
        Variant variant = Dispatch.call(dispatch, "LoadDataFromXML", new Variant(data));
        return variant.getBoolean();
    }

    @Override
    public void fnLoadDataListDatas(List<?> datas, Json json) {
        Map<String, Object> map = new HashMap<>();
        map.put("Detail", datas);
        this.fnLoadDataFromXML(json.toJSONString(map));
    }

    @Override
    public void fnExportImg(ImageType imageType, String fileName, boolean allInOne, boolean doneOpen) {
        Dispatch ExportOption = Dispatch.call(dispatch, "PrepareExport", new Variant(ExportType.gretIMG.getValue())).getDispatch();
        Dispatch E2IMGOption = Dispatch.get(ExportOption, "AsE2IMGOption").getDispatch();
        Dispatch.put(E2IMGOption, "ImageType", new Variant(imageType.getValue()));
        Dispatch.put(E2IMGOption, "AllInOne", new Variant(allInOne));
        Dispatch.put(E2IMGOption, "FileName", new Variant(fileName));
        Dispatch.call(dispatch, "Export", new Variant(doneOpen));
        Dispatch.call(dispatch, "UnprepareExport");
    }


    @Override
    public boolean fnExportDirect(ExportType exportType, String fileName, boolean showOptionDlg, boolean doneOpen) {
        Variant variant = Dispatch.call(dispatch, "ExportDirect", new Variant(exportType.getValue()), new Variant(fileName), new Variant(showOptionDlg), new Variant(doneOpen));
        return variant.getBoolean();
    }

    @Override
    public boolean fnLoadFromURL(String url) {
        Variant variant = Dispatch.call(dispatch, "LoadFromURL", new Variant(url));
        return variant.getBoolean();
    }

    @Override
    public boolean fnLoadFromFile(String fileName) {
        Variant variant = Dispatch.call(dispatch, "LoadFromFile", new Variant(fileName));
        return variant.getBoolean();
    }

    @Override
    public boolean fnLoadFromStr(String template) {
        Variant variant = Dispatch.call(dispatch, "LoadFromStr", new Variant(template));
        return variant.getBoolean();
    }

    @Override
    public void about() {
        Dispatch.call(dispatch, "About");
    }

}
